package com.car.slave.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.car.slave.im.IMClient;
import com.car.slave.im.model.user.IMUserMessage;
import com.car.slave.im.receiver.IMNotificationReceiver.IMNotificationReceiverCallback;
import com.imchezhil.R;

public class MainActivity extends Activity {
	
	private long mExitTime;
	
	private TextView number;
	
	private Button chat;
	private Button logout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		initViewData();
		initViewListener();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		
		loadUnReadCount();
	}
	
	private void initView() {
		chat = (Button) findViewById(R.id.chat);
		number = (TextView) findViewById(R.id.number);
		logout = (Button) findViewById(R.id.logout);
	}
	
	private void initViewData() {
		
	}
	
	private void initViewListener() {
		chat.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this, ChatActivity.class);
				startActivity(intent);
			}
		});
		
		logout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
				builder.setTitle("提示").setMessage("是否退出账号").setNegativeButton("否", null).
				setPositiveButton("是", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						IMClient.shareInstance().logout(MainActivity.this);
						// 跳到登陆界面
						Intent intent = new Intent(MainActivity.this, LoginActivity.class);
						intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
						startActivity(intent);
					}
				});
				builder.create().show();
			}
		});
		
		// 设置推送的回调.
		IMClient.shareInstance().setNotificationReceiverCallback(this, new IMNotificationReceiverCallback() {
			@Override
			public void onOtherLogin(Context context) {
				Toast.makeText(MainActivity.this, "异地登录", Toast.LENGTH_LONG).show();
				
				// 跳到登陆界面
				Intent intent = new Intent(MainActivity.this, AccountInfoDialogActivity.class);
				startActivity(intent);
			}
			
			@Override
			public void messageNotificater(Context context, IMUserMessage message) {
				Toast.makeText(MainActivity.this, "收到消息通知", Toast.LENGTH_LONG).show();
				loadUnReadCount();
			}
		});
	}
	
	private void loadUnReadCount() {
		int count = IMClient.shareInstance().getMessageUnReadCount(MainActivity.this);
		if (count == 0) {
			number.setVisibility(View.GONE);
		} else {
			number.setVisibility(View.VISIBLE);
		}
		number.setText(String.valueOf(count));
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if ((System.currentTimeMillis() - mExitTime) > 2000) {
				Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
				mExitTime = System.currentTimeMillis();
			} else {
				finish();
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
